package com.hustzp.com.xichuangzhu.dao;

import android.content.Context;
import android.util.Log;

import com.hustzp.com.xichuangzhu.model.Review;
import com.hustzp.com.xichuangzhu.utils.DBHelper;
import com.j256.ormlite.stmt.QueryBuilder;

import java.util.List;

/**
 * Created by fujiangren on 16/5/29.
 */

public class ReViewDao {


    private Context context;
    private DBHelper helper;

    public ReViewDao(Context context) {
        this.context = context;
        try {
            helper = DBHelper.getInstance(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Review> getAllReview() {
        try {
            List<Review> users = helper.getDao(Review.class).queryForAll();
            return users;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 进入应用摘录的查询 方法
     *
     * @param review
     * @param notinid
     * @return
     */

    public List<Review> getByReview(Review review, String notinid, Long limit) {
        try {
            //此种方式会出现重复数据，NOTIN没有起作用
            QueryBuilder<Review, Integer> builder = (QueryBuilder<Review, Integer>) helper.getDao(Review.class).queryBuilder();
            builder.orderByRaw("RANDOM()").limit(limit).where().ne("author_id", review.getAuthor_id())
                    .and()
                    .ne("wygtfs_error", review.getWygtfs_error())
                    .and()
                    .notIn("id", notinid);

            List<Review> reviews = helper.getDao(Review.class).query(builder.prepare());
            return reviews;
        } catch (Exception e) {
            Log.e("Exception", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }


    public List<Review> getLikeQuotes(List<Integer> quoteIds) {
        try {
            QueryBuilder<Review, Integer> builder = (QueryBuilder<Review, Integer>) helper.getDao(Review.class).queryBuilder();
            builder.where().in("id", quoteIds);
            List<Review> reviews = helper.getDao(Review.class).query(builder.prepare());
            return reviews;
        } catch (Exception e) {
            Log.e("Exception", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }


}
